<template>
  <el-dialog
    title="分配角色"
    :visible="showRoledialog"
    width="50%"
    @close="btnCancel"
  >
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="(item) in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-button @click="btnCancel">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  components: {},
  props: {
    showRoledialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      list: [],
      roleIds: []
    }
  },
  computed: {},
  watch: {
    userId(val) {
      this.getUserInfo(this.userId)
    }
  },
  created() {
    this.getRoled()
  },
  mounted() {},
  methods: {
    async getRoled() {
      const { rows } = await getRoles({ page: 1, pagesize: 20 })
      this.list = rows
    },
    async getUserInfo(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds
    },
    async btnCancel() {
      this.$emit('update:show-roledialog', false)
    },
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('分配成功')
      this.$emit('update:showRoledialog', false)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
